<?php

namespace app\admin\controller;

use app\admin\model\Money as MoneyModel;
use app\admin\model\Excels as ExcelsModel;

class Money extends Common
{
    //列表
    public function lst() {
        //分组查询并对花费字段求和
        $datas = ExcelsModel::field("username,sum(cost) cost_count")->group('username')->select();
        //把得到的数据添加到money表中
        $money = new MoneyModel();
        foreach ($datas as $v) {
            $data = [
                'username'      =>   $v['username'],
                'cost_count'    =>  $v['cost_count']
            ];
            $res = $money->where('username',$v['username'])->find();
            if ($res) {
                $res->save($data);
            }else {
                continue;
            }
        }

        return $this->fetch('lst');
    }

    //数据表格显示数据
    public function getMoneyDatas() {
        //获取请求参数
        $page = $this->request->param('page');
        $limit = $this->request->param('limit');

        $condition = $this->request->param('condition'); //查询的字段
        $content = $this->request->param('content'); //查询的内容

        $start = ($page-1)*$limit;

        if ($condition) {
            //搜索
            $data = MoneyModel::whereLike($condition,'%'.$content.'%')->limit($start,$limit)->select();
            $count = MoneyModel::whereLike($condition,'%'.$content.'%')->count();
        }else {
            $data = MoneyModel::limit($start,$limit)->select();
            $count = count(MoneyModel::all());
        }

        $datas = [
            'code'  =>  0,
            'msg'   =>  '查询成功',
            'data'  =>  $data,
            'count' =>  $count
        ];
        echo json_encode($datas);
        die;
    }

    //添加页面
    public function add() {
        if ($this->request->isAjax()) {
            $wx_name = $this->request->post('wx_name');
            $username = $this->request->post('username');
            $sex = $this->request->post('sex');
            $money_count = $this->request->post('money_count');

            $data = [
                'wx_name'       =>  $wx_name,
                'username'      =>  $username,
                'sex'           =>  $sex,
                'money_count'   =>  $money_count
            ];

            //添加数据
            $money = new MoneyModel();
            $res = $money->save($data);

            if ($res) {
                $this->success('添加成功~','admin/money/lst');
            }else {
                $this->error('添加失败！');
            }
        }

        return $this->fetch('add');
    }

    //修改
    public function edit() {
        //修改数据
        if ($this->request->isAjax()) {
            $id = $this->request->post('id');
            $wx_name = $this->request->post('wx_name');
            $username = $this->request->post('username');
            $sex = $this->request->post('sex');
            $money_count = $this->request->post('money_count');

            $datas = [
                'id'            =>  $id,
                'wx_name'       =>  $wx_name,
                'username'      =>  $username,
                'sex'           =>  $sex,
                'money_count'   =>  $money_count
            ];

            //更新数据
            $money = new MoneyModel();
            $res = $money->isUpdate(true)->save($datas);
            if ($res) {
                $this->success('修改成功~','admin/money/lst');
            }else {
                $this->error('修改失败！');
            }
        }

        //接收url传递的id
        $id = $this->request->param('id');
        $data = MoneyModel::get($id);
        $this->assign('data',$data);

        return $this->fetch('edit');
    }

    //删除
    public function del() {
        if ($this->request->isAjax()) {
            $id = $this->request->post('id');
            $res = MoneyModel::get($id)->delete();
            if ($res) {
                $this->success('删除成功~','admin/money/lst');
            }else {
                $this->error('删除失败！');
            }
        }
    }
}
